/**
 * @Date 2023-02-21 14:00:07
 * @Remark
 */

// lib
// config
import { MODULE_NAME } from "./config";
// script & methods & public
import logUtils from "utils/logUtils";
import toast from "components/toast";
// import timeUtils from "utils/timeUtils";
import { createFrontIndex } from "utils/numberUtils";
// store
import { state } from "./state";
// service
import {
  addUserService,
  // changeUserIdService,
  delUserService,
  getUserPageService,
  setUserStatusService,
  updateRemarkService,
  // updateUserDeliverService,
  // updateUserTypeService,
} from "biz/User/service";
import { getShopNoPageService } from "biz/System/service";
// interface
// 其它

// 用户管理-列表分页
export async function getUserPage() {
  const sp = state.getSearch();
  logUtils.info("搜索参数", sp);
  // const date = timeUtils.formatRangeDate(sp.sDate);
  // 调用服务
  state.loading = true;
  const result = await getUserPageService({
    pageNum: state.page,
    pageSize: state.size,
    ...sp
    // identityId: sp.sId,
    // userName: sp.sName,
    // mobile: sp.sMobile,
    // identityNumber: sp.sIdcard,
    // startDate: date[0],
    // endDate: date[1],
    // teamId: sp.sTeam,
    // nickName: sp.sNickname,
    // isTeam: sp.sIsTeam,
  });
  state.loading = false;
  // 成功
  if (result.payload) {
    state.total = result.payload.total;
    state.list = result.payload.list.map((el, index) => ({
      ...el,
      frontIndex: createFrontIndex(index, state.page, state.size),
    }));
  }
}

// 用户管理-修改备注
export async function updateRemark(form: any, id: string) {
  // 调用服务
  state.editModalLoading = true;
  const result = await updateRemarkService({
    id,
    ...form,
  });
  state.editModalLoading = false;
  // 成功
  if (result.payload) {
    toast.success("编辑成功");
    state.remarkEditModal = false;
    getUserPage();
  }
}

// 用户管理-店铺不分页
export async function getShopNoPage() {
  // 调用服务
  const result = await getShopNoPageService();
  // 成功
  if (result.payload) {
    state.shopList = result.payload;
  }
}

export async function addUser(form: any) {
  if (form.pass !== form.pwd) {
    toast.error("两次密码输入不一致");
    return;
  }
  logUtils.info(`${MODULE_NAME}页面表单`, form);
  // 调用服务
  state.editModalLoading = true;
  const result = await addUserService({
    ...form,
  });
  state.editModalLoading = false;
  // 成功
  if (result.payload) {
    toast.success("添加成功");
    state.editModal = false;
    getUserPage();
  }
}

export async function delUser(id: string) {
  // 调用服务
  const result = await delUserService(id);
  // 成功
  if (result.payload) {
    toast.success("删除成功");
    getUserPage();
  }
}

export async function setUserStatus(id: string, status: string) {
  // 调用服务
  const result = await setUserStatusService({
    id,
    statusEnum: status,
  });
  // 成功
  if (result.payload) {
    toast.successAlert("set");
    getUserPage();
  }
}

// export async function changeUserId(form: any, id: string) {
//   if (form.identityId !== form.identityIdCheck) {
//     toast.error("两次ID输入不一致");
//     return;
//   }
//   logUtils.info(`${MODULE_NAME}页面表单`, form);
//   // 调用服务
//   state.idModalLoading = true;
//   const result = await changeUserIdService({
//     id,
//     ...form,
//   });
//   state.idModalLoading = false;
//   // 成功
//   if (result.payload) {
//     toast.success("修改ID成功");
//     state.idModal = false;
//     getUserPage();
//   }
// }

export async function didMount() {
  getUserPage();
  // getShopNoPage();
}
